package org.dbi.beans;

import java.util.HashMap;

import org.dbi.exceptions.KeyAlreadyExistsException;
import org.dbi.exceptions.MissingKeyException;
import org.dbi.logger.ErrorLogger;

public class Constraint {
	private String naam = "";
	private HashMap<String, Key> keys = new HashMap<String, Key>();
	private HashMap<String, Reference> refs = new HashMap<String, Reference>();
	
	private ErrorLogger logger = ErrorLogger.getLogger(getClass().getName());
	
	public Constraint(String naam) {
		this.naam = naam;
		logger.info("Constraint: " + naam + " aangemaakt.");
	}
	
	public String getNaam() {
		return naam;
	}
	
	public HashMap<String, Key> getKeys() {
		return keys;
	}
	
	public HashMap<String, Reference> getRefs() {
		return refs;
	}
	
	public void addSingleKeys(Key key) {
		
		if (!keys.containsKey(key.getNaam())) {
			keys.put(key.getNaam(), key);
			logger.info("Key: " + key.getNaam() + " toegevoegd aan Constraint: " + getNaam());
		}
		
	}
	
	public Key getKey(String keyNaam) {
		Key k = null;
		try {
			if (keys.containsKey(keyNaam)) {
				k = keys.get(keyNaam);
			} else
				throw new MissingKeyException(keyNaam);
		} catch (MissingKeyException mke) {
			logger.severe(mke.getMessage());
		}
		return k;
		
	}
	
	public void addMultipleKeys(HashMap<String, Key> keys) {
		if (keys.size() != 0 && keys != null) {
			for (Key key : keys.values()) {
				addSingleKeys(key);
			}
		}
	}
	
	public boolean addsingleReference(Reference ref) {
		if (!refs.containsKey(ref.getNaam())) {
			logger.info("Reference: " + ref.getNaam() + " " + ref.getFromGroup().getNaam() + " toegevoegd aan Constraint: " + getNaam());
			refs.put(ref.getNaam(), ref);
			return true;
		} else {
			return false;
		}
	}
	
	public void addMultipleRefs(HashMap<String, Reference> refs) {
		if (refs.size() != 0 && refs != null) {
			for (Reference ref : refs.values()) {
				addsingleReference(ref);
			}
		}
	}
	
}
